using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace BookShop
{
	/// <summary>
	/// Strongly-typed collection for the Purchase class.
	/// </summary>
    [Serializable]
	public partial class PurchaseCollection : ActiveList<Purchase, PurchaseCollection>
	{	   
		public PurchaseCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>PurchaseCollection</returns>
		public PurchaseCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Purchase o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Purchase table.
	/// </summary>
	[Serializable]
	public partial class Purchase : ActiveRecord<Purchase>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Purchase()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Purchase(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Purchase(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Purchase(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Purchase", TableType.Table, DataService.GetInstance("BookShop"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarPurchaseID = new TableSchema.TableColumn(schema);
				colvarPurchaseID.ColumnName = "PurchaseID";
				colvarPurchaseID.DataType = DbType.AnsiStringFixedLength;
				colvarPurchaseID.MaxLength = 15;
				colvarPurchaseID.AutoIncrement = false;
				colvarPurchaseID.IsNullable = false;
				colvarPurchaseID.IsPrimaryKey = true;
				colvarPurchaseID.IsForeignKey = false;
				colvarPurchaseID.IsReadOnly = false;
				colvarPurchaseID.DefaultSetting = @"";
				colvarPurchaseID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPurchaseID);
				
				TableSchema.TableColumn colvarSupplierID = new TableSchema.TableColumn(schema);
				colvarSupplierID.ColumnName = "SupplierID";
				colvarSupplierID.DataType = DbType.AnsiStringFixedLength;
				colvarSupplierID.MaxLength = 10;
				colvarSupplierID.AutoIncrement = false;
				colvarSupplierID.IsNullable = false;
				colvarSupplierID.IsPrimaryKey = false;
				colvarSupplierID.IsForeignKey = true;
				colvarSupplierID.IsReadOnly = false;
				colvarSupplierID.DefaultSetting = @"";
				
					colvarSupplierID.ForeignKeyTableName = "Supplier";
				schema.Columns.Add(colvarSupplierID);
				
				TableSchema.TableColumn colvarPurchaseDate = new TableSchema.TableColumn(schema);
				colvarPurchaseDate.ColumnName = "PurchaseDate";
				colvarPurchaseDate.DataType = DbType.DateTime;
				colvarPurchaseDate.MaxLength = 0;
				colvarPurchaseDate.AutoIncrement = false;
				colvarPurchaseDate.IsNullable = false;
				colvarPurchaseDate.IsPrimaryKey = false;
				colvarPurchaseDate.IsForeignKey = false;
				colvarPurchaseDate.IsReadOnly = false;
				colvarPurchaseDate.DefaultSetting = @"";
				colvarPurchaseDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPurchaseDate);
				
				TableSchema.TableColumn colvarTotalPrice = new TableSchema.TableColumn(schema);
				colvarTotalPrice.ColumnName = "TotalPrice";
				colvarTotalPrice.DataType = DbType.Currency;
				colvarTotalPrice.MaxLength = 0;
				colvarTotalPrice.AutoIncrement = false;
				colvarTotalPrice.IsNullable = false;
				colvarTotalPrice.IsPrimaryKey = false;
				colvarTotalPrice.IsForeignKey = false;
				colvarTotalPrice.IsReadOnly = false;
				colvarTotalPrice.DefaultSetting = @"";
				colvarTotalPrice.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTotalPrice);
				
				TableSchema.TableColumn colvarTotalCost = new TableSchema.TableColumn(schema);
				colvarTotalCost.ColumnName = "TotalCost";
				colvarTotalCost.DataType = DbType.Currency;
				colvarTotalCost.MaxLength = 0;
				colvarTotalCost.AutoIncrement = false;
				colvarTotalCost.IsNullable = false;
				colvarTotalCost.IsPrimaryKey = false;
				colvarTotalCost.IsForeignKey = false;
				colvarTotalCost.IsReadOnly = false;
				colvarTotalCost.DefaultSetting = @"";
				colvarTotalCost.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTotalCost);
				
				TableSchema.TableColumn colvarTotalAmount = new TableSchema.TableColumn(schema);
				colvarTotalAmount.ColumnName = "TotalAmount";
				colvarTotalAmount.DataType = DbType.Int32;
				colvarTotalAmount.MaxLength = 0;
				colvarTotalAmount.AutoIncrement = false;
				colvarTotalAmount.IsNullable = true;
				colvarTotalAmount.IsPrimaryKey = false;
				colvarTotalAmount.IsForeignKey = false;
				colvarTotalAmount.IsReadOnly = false;
				colvarTotalAmount.DefaultSetting = @"";
				colvarTotalAmount.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTotalAmount);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["BookShop"].AddSchema("Purchase",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("PurchaseID")]
		[Bindable(true)]
		public string PurchaseID 
		{
			get { return GetColumnValue<string>(Columns.PurchaseID); }
			set { SetColumnValue(Columns.PurchaseID, value); }
		}
		  
		[XmlAttribute("SupplierID")]
		[Bindable(true)]
		public string SupplierID 
		{
			get { return GetColumnValue<string>(Columns.SupplierID); }
			set { SetColumnValue(Columns.SupplierID, value); }
		}
		  
		[XmlAttribute("PurchaseDate")]
		[Bindable(true)]
		public DateTime PurchaseDate 
		{
			get { return GetColumnValue<DateTime>(Columns.PurchaseDate); }
			set { SetColumnValue(Columns.PurchaseDate, value); }
		}
		  
		[XmlAttribute("TotalPrice")]
		[Bindable(true)]
		public decimal TotalPrice 
		{
			get { return GetColumnValue<decimal>(Columns.TotalPrice); }
			set { SetColumnValue(Columns.TotalPrice, value); }
		}
		  
		[XmlAttribute("TotalCost")]
		[Bindable(true)]
		public decimal TotalCost 
		{
			get { return GetColumnValue<decimal>(Columns.TotalCost); }
			set { SetColumnValue(Columns.TotalCost, value); }
		}
		  
		[XmlAttribute("TotalAmount")]
		[Bindable(true)]
		public int? TotalAmount 
		{
			get { return GetColumnValue<int?>(Columns.TotalAmount); }
			set { SetColumnValue(Columns.TotalAmount, value); }
		}
		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Supplier ActiveRecord object related to this Purchase
		/// 
		/// </summary>
		public BookShop.Supplier Supplier
		{
			get { return BookShop.Supplier.FetchByID(this.SupplierID); }
			set { SetColumnValue("SupplierID", value.SupplierID); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varPurchaseID,string varSupplierID,DateTime varPurchaseDate,decimal varTotalPrice,decimal varTotalCost,int? varTotalAmount)
		{
			Purchase item = new Purchase();
			
			item.PurchaseID = varPurchaseID;
			
			item.SupplierID = varSupplierID;
			
			item.PurchaseDate = varPurchaseDate;
			
			item.TotalPrice = varTotalPrice;
			
			item.TotalCost = varTotalCost;
			
			item.TotalAmount = varTotalAmount;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(string varPurchaseID,string varSupplierID,DateTime varPurchaseDate,decimal varTotalPrice,decimal varTotalCost,int? varTotalAmount)
		{
			Purchase item = new Purchase();
			
				item.PurchaseID = varPurchaseID;
			
				item.SupplierID = varSupplierID;
			
				item.PurchaseDate = varPurchaseDate;
			
				item.TotalPrice = varTotalPrice;
			
				item.TotalCost = varTotalCost;
			
				item.TotalAmount = varTotalAmount;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn PurchaseIDColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn SupplierIDColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn PurchaseDateColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn TotalPriceColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn TotalCostColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn TotalAmountColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string PurchaseID = @"PurchaseID";
			 public static string SupplierID = @"SupplierID";
			 public static string PurchaseDate = @"PurchaseDate";
			 public static string TotalPrice = @"TotalPrice";
			 public static string TotalCost = @"TotalCost";
			 public static string TotalAmount = @"TotalAmount";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
